#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "cfglst.h"


ptr_cfglst cfglst_init(void)
{
    return NULL;
}

ptr_cfglst cfglst_newnode(void)
{
    ptr_cfglst list = (ptr_cfglst) xmalloc(sizeof(cfglst));
    list->next = NULL;
    return list;
}

void cfglst_append(ptr_cfglst *head,
                   char *pattern, char **dest, size_t dest_depth)
{
    if (*head == NULL) {
        *head = cfglst_newnode();
        (*head)->pattern = pattern;
        (*head)->dest = dest;
        (*head)->dest_depth = dest_depth;
    }
    else {
        ptr_cfglst tmp = *head;
        while (tmp->next) {
            tmp = tmp->next;
        }
        tmp->next = cfglst_newnode();
        tmp->next->pattern = pattern;
        tmp->next->dest = dest;
        tmp->next->dest_depth = dest_depth;
    }
}

void cfglst_free(ptr_cfglst head)
{
    assert(head);
    while (head) {
        ptr_cfglst next = head->next;
        free(head);
        head = next;
    }
}
